package 链表;

import java.util.*;

public class NodeUtil {

    public static Node createNodeList(List<Integer> nodeValue){
        Node head = new Node();
        Node prev = head;
        head.setValue(nodeValue.get(0));
        Map<Integer,Node> hashMap = new HashMap<>();
        for (int i = 1; i < nodeValue.size(); i++) {
            Node node = new Node();
            node.setValue(nodeValue.get(i));
            if (hashMap.containsKey(nodeValue.get(i))){
                prev.setTail(hashMap.get(nodeValue.get(i)));
                break;
            }else{
                hashMap.put(nodeValue.get(i), node);
            }
            prev.setTail(node);
            prev = node;
        }
        return head;
    }



    public static int length(Node node){
        int count  = 0;
        while (node != null){
            count ++;
            node = node.getTail();
        }
        return count;

    }
}
